<template>
  <div class="public-wedding-container" ref="wcWaterFallRef" @scroll="handleScroll">
    <wc-waterfall gap="6" :cols="cols">
      <div class="pic-card" v-for="(item, index) in pictures" :key="index"  @dblclick="handleDownloadImg(item)">
        <el-image :src="item"/>
      </div>
    </wc-waterfall>
    <div class="act-wrap">
      <span @click="downloadImages">下载<br/>全部</span>
    </div>
  </div>
</template>

<script lang="ts">
import {toRefs, reactive, defineComponent, onBeforeMount, ref} from 'vue';
import 'wc-waterfall';

export default defineComponent({
  name: 'apiPublicDocPage',
  setup() {
    const wcWaterFallRef = ref(null);
    const state = reactive({
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      total: 0,
      cols: 4,
      downLoaded: [],
      pictures: [
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH01.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH02.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH03.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH04.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH05.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH06.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH07.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH08.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH09.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH010.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH011.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH012.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH013.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH014.jpg',
       'https://jadmin.wocoding.cn/yd3hyh873/public/20240531/lbg_113422oBswH015.jpg',
      ]
    });
    onBeforeMount(() => {
      handleWindowResize();
      window.addEventListener('resize', () => {
        handleWindowResize();
      });
    });
    const handleWindowResize = () => {
      const _w = window.innerWidth;
      if (_w <= 720 && _w >= 560) {
        state.cols = 2;
      } else if (_w < 560) {
        state.cols = 1;
      } else {
        state.cols = 4;
      }
    }
    const queryPage = () => {
      console.log("kkkkkkkkkkkkkk")
    }
    const handleScroll = () => {
      const scrollWrap = wcWaterFallRef.value;
      const {scrollTop, clientHeight, scrollHeight} = scrollWrap;
      if (scrollTop + clientHeight >= scrollHeight - 10) {
        if (Math.ceil(state.total / state.queryParams.pageSize) > state.queryParams.pageNum) {
          state.queryParams.pageNum += 1;
          queryPage();
        }
      }
    };
    const downloadImages = () => {
      state.pictures.forEach((item, index) => {
        if (!state.downLoaded.includes(item)) {
          handleDownloadImg(item);
        }
      })
    };
    const handleDownloadImg = (_src: any) => {
      console.log("xxxxxxxxxxxxxxx")
      state.downLoaded.push(_src);
      let image = new Image()
      // 解决跨域 Canvas 污染问题
      image.setAttribute('crossOrigin', 'anonymous')
      let names = 'image' + _src.split('/').pop()
      console.log(names, 'names')
      image.src = _src
      image.onload = function () {
        let canvas = document.createElement('canvas')
        canvas.width = image.width
        canvas.height = image.height
        let context = canvas.getContext('2d')
        context.drawImage(image, 0, 0, image.width, image.height)
        let url = canvas.toDataURL('image/jpeg') //得到图片的base64编码数据
        let a = document.createElement('a') // 生成一个a元素
        let event = new MouseEvent('click') // 创建一个单击事件
        a.download = names || 'photo' // 设置图片名称
        a.href = url // 将生成的URL设置为a.href属性
        a.dispatchEvent(event) // 触发a的单击事件
      }
      image.src = _src
    };
    return {
      wcWaterFallRef,
      ...toRefs(state),
      handleScroll,
      downloadImages,
      handleDownloadImg
    };
  },
});
</script>
<style scoped lang="scss">
.public-wedding-container {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 6px;
  position: relative;

  .pic-card {
    overflow-x: hidden;
    overflow-y: auto;
    border-radius: 6px;

    img {
      width: 100%;
      border-radius: 6px;
      height: 100%;
    }
  }

  .act-wrap {
    position: fixed;
    right: 10px;
    bottom: 60px;
    text-align: center;vertical-align: center;

    span {
      width: 50px;
      height: 50px;
      display: block;
      border-radius: 50%;
      background-color: #b5276c;
      color: #efefef;
      font-size: 12px;
      cursor: pointer;
      padding-top: 9px;
    }
  }
}
</style>
